﻿package net.wangit.adm.group;

import net.wangit.context.ActionContext;
import net.wangit.jdbc.ColumnRow;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.Table;
import net.wangit.support.action.TransactionAction;
import net.wangit.user.admin.AdminCache;
import net.wangit.user.group.UserGroupCache;
import net.wangit.util.Checker;

public class GroupAuthAction extends TransactionAction {

	private static final String SQL_CLEAR = "delete from AGROUPROLE where groupid=?";

	public GroupAuthAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		String groupId = ac.getRequestParameter("group.roles.groupid");
		if (Checker.isEmpty(groupId))
			return;
		db.execute("delete from AGROUPROLE where groupid=?", groupId, 2);
		String checks[] = ac.getRequestParameters("_treecheck");
		if (!Checker.isEmpty(checks)) {
			Table table = new Table("adm", "AGROUPROLE");
			for (int i = 0; i < checks.length; i++) {
				ColumnRow row = table.addInsertRow();
				row.set("groupid", groupId);
				row.set("roleid", checks[i]);
			}

			db.save(table);
		}
		UserGroupCache.clear(groupId);
		AdminCache.clearAll();
	}
}
